package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //显示列表
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, String name) {
        //调用service
        Page<Setmeal> setmealPage = setmealService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(setmealPage);
    }

    //添加套餐
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal) {
        //调用service
        setmealService.save(setmeal);
        return ResultInfo.success();
    }

    //修改回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        // 调用service
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    //修改套餐
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal) {
        //调用service
        setmealService.update(setmeal);
        return ResultInfo.success();
    }

    //启停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatusSetmeal(@PathVariable(value = "status") Integer status,
                                          @RequestParam List<Long> ids) {
        //调用service
        setmealService.updateStatusSetmeal(status, ids);
        return ResultInfo.success();
    }

    //删除
    @DeleteMapping("/setmeal")
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) {
        //调用service
        setmealService.deleteBatch(ids);
        return ResultInfo.success();
    }
}
